package com.longcai.cm.moudules.system.controller;

import com.longcai.cm.base.aop.Log;
import com.longcai.cm.base.bean.PageVo;
import com.longcai.cm.base.bean.Pageable;
import com.longcai.cm.bean.R;
import com.longcai.cm.common.utils.RequestUtil;
import com.longcai.cm.moudules.system.domain.SysConfig;
import com.longcai.cm.moudules.system.service.SysConfigService;
import com.longcai.cm.utils.DateUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @Description: sys_config
 * @file:
 * @throws
 * @date 2020-01-10
 */
@RestController
@RequestMapping("/sysConfig")
@Api(value = "系统管理-参数配置", tags = "系统管理-参数配置")
@ApiSort(8)
public class SysConfigController {


    @Resource
    private SysConfigService sysConfigService;


    /**
     * 【API】---- 获取系统参数设置列表
     *
     * @param request  [请求信息]
     * @param response [响应信息]
     * @return R
     */
    @PostMapping("/getList")
    @ApiOperation(value = "【S7001】获取系统参数设置列表", notes = "获取系统参数设置列表")
    @ApiOperationSupport(order = 1)
    @Log(value = "获取系统参数设置列表", type = "1")
    public R<PageVo<SysConfig>> getList(HttpServletRequest request, HttpServletResponse response) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<SysConfig> result = sysConfigService.getPageBean(pageInfo, SysConfig.class);
        return R.success(result);
    }


    /**
     * 保存系统参数设置
     *
     * @param vo       系统参数实体
     * @param request  [请求信息]
     * @param response [响应信息]
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "【S7002】保存系统参数设置", notes = "系统参数设置")
    @ApiOperationSupport(order = 2)
    @Log(value = "保存系统参数设置", type = "1")
    public R save(SysConfig vo, HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        if (id != null && id != "") {
            vo.setUpdateTime(DateUtils.getTimeNow());
            sysConfigService.updateById(vo);
        } else {
            vo.setId(UUID.randomUUID().toString().replace("-", ""));
            vo.setCreateTime(DateUtils.getTimeNow());
            sysConfigService.save(vo);
        }

        return R.success();
    }


    /**
     * 【API】----  删除多个数据
     *
     * @param request  [请求信息]
     * @param response [响应信息]
     * @return R
     */
    @PostMapping("/delete")
    @ApiOperation(value = "【S7004】批量删除系统参数设置", notes = "批量删除系统参数设置")
    @ApiOperationSupport(order = 4)
    @Log(value = "删除系统参数设置", type = "1")
    public R delete(HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");
        String ids = request.getParameter("ids");
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            sysConfigService.removeById(idString);
        }
        return R.success();
    }

}
